//
//  HomeViewController.swift
//  Swift训练
//
//  Created by zhenjingxiong on 2018/8/30.
//  Copyright © 2018年 zhenjingxiong. All rights reserved.
//

import UIKit
import SnapKit
import ESPullToRefresh
import Alamofire
import LLCycleScrollView

class HomeViewController: BaseViewController,UITableViewDelegate,UITableViewDataSource,LLCycleScrollViewDelegate{
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.layoutUI()

        let postUrl =  "http://www.weather.com.cn/data/sk/101010100.html"
        
        //        let param : [String : Any]? = ["userId" : userId]
        
        Alamofire.request(postUrl, method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil).responseJSON { (response) in
            print(response)
        }

        HomeViewController.sanHi()
        
        
//        let array = [1,2,3,4,5]
//        sum(array) // 15
//
//        print(sum(array))
        
//        var someString = "hello"
//        var anotherString = "world"
//        swapTwoValues(&someString, &anotherString)
//
        var a = 3
        var b = 4
        
        
        swap(&a, &b)
        print(a,b)
        
        let arr = [1,2,3]
        let arr2 = arr.map ({
            $0 + 2
        })
        print("arr2的结果是\(arr2)")

        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //MARK:action事件
    
    @objc override func clickLeft() {
        print("点击左边")
    }
    
    @objc func clickRight() {
        print("点击右边")
        let NewCustomerVC = NewCustomerViewController()
        self.navigationController?.pushViewController(NewCustomerVC, animated: true)
        
    }
    
    
    //MARK:tablView的代理方法
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.itemArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell:ItemCell = (tableView.dequeueReusableCell(withIdentifier: "Item") as? ItemCell)!
        cell.titleLable.text = titleArray[indexPath.row]
        cell.leftImageView.image = UIImage.init(named: self.leftImageNameStrArray[indexPath.row])
        return cell
        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        switch indexPath.row {
        case 0:
            let ToBePerferctVC = ToBePerfectViewController()
          self.navigationController?.pushViewController(ToBePerferctVC, animated: true)
        case 1:
            let ToFollowUpVC = ToFollowUpViewController()
            self.navigationController?.pushViewController(ToFollowUpVC, animated: true)
        case 2:
            let WillBeOverueVC = WillBeOverdueViewController()
            self.navigationController?.pushViewController(WillBeOverueVC, animated: true)
        default:
            let OrderVC = OrderViewController()
            self.navigationController?.pushViewController(OrderVC, animated: true)
        }
        
        tableView.deselectRow(at: indexPath, animated: true)
    }
    
    //MARK:CycleScrollView的代理方法
    func cycleScrollView(_ cycleScrollView: LLCycleScrollView, didSelectItemIndex index: NSInteger) {
        
        print("协议:当前点击文本的位置:\(index)")
    }
   
    //MARK: 创建UI
    func layoutUI()  {
        self.navigationItem.titleView = self.titleView
        self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(image: UIImage.init(named: "icon_message"), style: UIBarButtonItemStyle.plain, target: self, action:#selector(clickLeft))
        self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(image: UIImage.init(named: "icon_add"), style: UIBarButtonItemStyle.plain, target: self, action: #selector(clickRight))
        //添加titleView的子试图
        self.addSubViewToTitleView()
        
        //添加tableView到self.View上
        self.view.addSubview(self.listView)
        self.listView.snp.makeConstraints { (make) in
            make.edges.equalTo(self.view)
        }
        
        self.listView.tableHeaderView = self.headView
        
        //添加轮播图
        self.addShufflingFigure()
        
        self.listView.register(ItemCell.self, forCellReuseIdentifier: "Item")
        
        var header: ESRefreshProtocol & ESRefreshAnimatorProtocol
        var footer: ESRefreshProtocol & ESRefreshAnimatorProtocol
        
        header = ESRefreshHeaderAnimator.init(frame: CGRect.zero)
        footer = ESRefreshFooterAnimator.init(frame: CGRect.zero)
        
        self.listView.es.addPullToRefresh(animator: header) { [weak self] in
            let delay = DispatchTime.now() + 5
            print("添加了一个5秒后执行的任务")
            DispatchQueue.main.asyncAfter(deadline: delay, execute: {
                self?.listView.es.stopPullToRefresh()
            })
            
        }
        
        self.listView.es.addInfiniteScrolling(animator: footer) { [weak self] in
            let delay = DispatchTime.now() + 5
            print("添加了一个5秒后执行的任务")
            DispatchQueue.main.asyncAfter(deadline: delay, execute: {
                self?.listView.es.stopLoadingMore()
            })
        }
    }
    
    func addShufflingFigure()  {
        
        // 网络图，本地图混合
        //        let imagesURLStrings = [
        //            "s3.jpg",
        //            "http://www.g-photography.net/file_picture/3/3587/4.jpg",
        //            "http://img2.zjolcdn.com/pic/0/13/66/56/13665652_914292.jpg",
        //            "http://c.hiphotos.baidu.com/image/w%3D400/sign=c2318ff84334970a4773112fa5c8d1c0/b7fd5266d0160924c1fae5ccd60735fae7cd340d.jpg",
        //            "http://img3.redocn.com/tupian/20150806/weimeisheyingtupian_4779232.jpg",
        //            ];
        
        let imagesURLStrings = [
            "s3.jpg","s2","s1"
        ];
        
        self.headView.addSubview(self.storyBoardBanner)
        
        // Storyboard Demo
        self.storyBoardBanner.imagePaths = imagesURLStrings
        //        self.storyBoardBanner.titles = titles
        
        // 新增图片显示控制
        self.storyBoardBanner.imageViewContentMode = .scaleToFill
        self.storyBoardBanner.customPageControlStyle = .image
        self.storyBoardBanner.pageControlPosition = .left
        
        // 是否对url进行特殊字符处理
        self.storyBoardBanner.isAddingPercentEncodingForURLString = true
   
        // 2018-02-25 新增协议
        self.storyBoardBanner.delegate = self
        
    }
    
    lazy var titleLabel = { () -> UILabel in
        
        let l = UILabel.init()
        l.text = "佛山金域缇香"
        l.textColor = UIColor.black
        l.font = UIFont.systemFont(ofSize: 16)
        //        l.backgroundColor = UIColor.red
        return l
    }()
    
    lazy var titleView = { () -> UIView in
        let view = UIView.init(frame: CGRect(x: 0, y: 0, width: 120, height: 40))
        view.backgroundColor = UIColor.green
        let tapOne = UITapGestureRecognizer.init(target: self, action: #selector(tapTitleView))
        view.addGestureRecognizer(tapOne)
        return view
    }()
    
    //下拉图片
    lazy var pullDownImageView = { () -> UIImageView in
        let imageView = UIImageView.init(image: UIImage.init(named: "icon_arrow_down"))
        //        imageView.backgroundColor = UIColor.black
        return imageView
    }()
    
    //数组
    lazy var itemArray = { () -> [String] in
        let array = ["待完善","待跟进","将逾期","预约跟客"]
        return array
    }()
    
    //tableView的初始化
    lazy var listView: UITableView = {
        let tableView = UITableView.init()
        tableView.backgroundColor = UIColor.white
        tableView.delegate = self
        tableView.dataSource = self
        tableView.rowHeight = 50
        tableView.separatorStyle = UITableViewCellSeparatorStyle.none
        tableView.separatorColor = UIColor.clear
        return tableView
    }()
    
    //添加覆盖的View试图
    lazy var coverView = { () -> CoverView in
        let view = CoverView()
        view.backgroundColor = UIColor.black.withAlphaComponent(0.4)
        return view
    }()
    
    func addSubViewToTitleView() {
        self.titleView.addSubview(self.titleLabel)
        self.titleLabel.snp.makeConstraints { (make) in
            make.left.top.bottom.equalTo(titleView)
        }
        
        self.titleView.addSubview(self.pullDownImageView)
        self.pullDownImageView.snp.makeConstraints { (make) in
            make.left.equalTo((self.titleLabel.snp.right)).offset(0)
            make.centerY.equalTo(self.titleLabel)
        }
    }
    
    @objc func tapTitleView()  {
        print("点击下titleView究竟是什么鬼")
        self.view.addSubview(self.coverView)
        self.coverView.snp.makeConstraints { (make) in
            make.edges.equalTo(self.view)
        }
    }
    
    
    lazy var titleArray: Array = { () -> [String] in
        let titleArray = ["待完善","待跟进","将逾期","预约跟客"]
        return titleArray
    }()
    
    lazy var leftImageNameStrArray: Array = { () -> [String] in
        let leftImageNameStrArray = ["icon_toBe","icon_daigenjin","icon_genjin","icon_yuyue"]
        return leftImageNameStrArray
    }()
    
    
    lazy var headView: HeadView = {
        let headView = HeadView.init(frame: CGRect.init(x: 0, y: 0, width:ScreenHeight, height: 200))
        return headView
    }()
    
    lazy var storyBoardBanner: LLCycleScrollView = {
        let storyBoardBanner = LLCycleScrollView.llCycleScrollViewWithFrame(CGRect.init(x: 0, y: 0, width: 375, height: 200))
        return storyBoardBanner
    }()
    
    
    
    // 类方法
    class func sanHi() {
        print("这个是类型方法")
    }

    
//    func  myswap<T>( a: inout T, b: inout T) {
//        let temp  = a
//        a = b
//        b = temp
//    }
//
    
//    func swapTwoInts(_ a: inout Int, _ b: inout Int) {
//        let temporaryA = a
//        a = b
//        b = temporaryA
//    }
//
    
    func swapTwoValues<T>(_ a: inout T, _ b: inout T) {
        let temporaryA = a
        a = b
        b = temporaryA
    }

    
    
    
   
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */
    
}

